<template>
  <el-form ref="deptForm" :model="form"  :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
     		<el-option 
            v-for="item in list"
            :key="item.id"
            :value="item.username" 
            :label="item.username" />
    
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {getEmployeeSimple} from '@/api/employees'
import {addDepartment,getDeparDetail,updateDepartments} from '@/api/departments'
export default {
  props:{
    originList:{
     type:Array,
     required:true
    },
   id:{
    type:String,
    required:true
  },
   isEdit:{
    type:Boolean,
    required:true
  }
  },
    data() {
    return {
        list: [], // 存储部门负责人
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules:{
        name: [
       {required:true,message:'请输入部门名称',trigger:'blur'},
       {min:1,man:50,message:'请输入1-50位字符',trigger:'blur'}
      ], 
        code: [
       {required:true,message:'请输入部门编码',trigger:'blur'},
       {min:1,man:50,message:'请输入1-50位字符',trigger:'blur'}
      ], 
        manager: [
       {required:true,message:'请输入部门负责人',trigger:'change'}
      ], 
        introduce: [
       {required:true,message:'请输入部门介绍',trigger:'blur'},
       {min:1,man:300,message:'请输入1-300位字符',trigger:'blur'}
      ] 
      }
    }
  },
  created() {
    this.loadEmployee()
    this.loadDetail()
  },
  methods: {
    // 更新当前部门数据
    async doEdit(){
    const res =  await updateDepartments(this.form)
    console.log(res,'resssss');
      this.$emit('success')
    },
    // 获取当前部门的详情
    async loadDetail(){
     if(this.isEdit){
      const res = await getDeparDetail(this.id)
      console.log('res',res.data);
      this.form = res.data
    }
    },
    // 获取部门负责人列表
    async loadEmployee() {
      const res = await getEmployeeSimple()
      console.log('部门负责人', res)
      this.list = res.data
    },
    // 实现添加子部门
    async doAdd(){
     try {
      const res = await addDepartment({pid:this.id, ...this.form})
     // 子传父
     this.$emit('success')
     } catch (error) {
      console.log(error);
     }
    },
    // 确定
    hSubmit() {
      this.$refs.deptForm.validate((valid)=>{
       if(!valid) return
      this.isEdit ? this.doEdit() :  this.doAdd()

      })
    },
    // 取消按钮
    hCancel() {
     // 子传父,关闭弹层
     this.$emit('close')

    }
  }
}
</script>

<style>

</style>